/*
 * GridChart.java
 * Android-Charts
 *
 * Created by limc on 2011/05/29.
 *
 * Copyright 2011 limc.cn All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zlwy.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.zlwy.activity.R;
import com.zlwy.entity.Grid;
import com.zlwy.entity.YBEntity;
import com.zlwy.util.Util;

/**
 * 分时图类
 * GridChart是所有网格图表的基础类对象，
 * 它实现了基本的网格图表功能，
 * 这些功能将被它的继承类使用
 */
public class GridChart extends View{

	public static final int DEFAULT_BACKGROUND_COLOR = Color.WHITE;//默认背景色
	private int axisColor = Color.GRAY;//坐标颜色
	private Grid grid;
	private float width;//图表的宽度
	private float height;//图表的高度
//	private float startX = axisMarginLeft+1;//起始X值
	private float startX = 0f;
	private float startY = 0.0f;//起始Y值
	private float chartWidth = 0;
	private VectorLineView vctView;
	private float scrollX = 0f;
	private Context context;
	//数据集合
	private List<YBEntity> datas = new ArrayList<YBEntity>();
	public GridChart(Context context) {
		super(context);	
		
	}
	
	public GridChart(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public void setAxisColor(int color){
		axisColor = color;
	}
	
	public void setVctView(VectorLineView vctView){
		this.vctView = vctView;
	}
	/**
	 * 设置数据，重绘
	 * @param grid
	 */
	public void setGrid(Grid grid){
		this.grid = grid;
		this.invalidate();
	}
	
	public void setData(List<YBEntity> datas){
		this.datas = datas;
		this.invalidate();
	}
	
	public void setContext(Context context){
		this.context = context;
	}
	
	/**
	 * 绘图方法，继承自view
	 */
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		width = super.getWidth();
		height = super.getHeight();
		setAxisColor(Color.GRAY);
		
		drawXAxis(canvas);
		drawYAxis(canvas);
		drawTopAxis(canvas);
		drawRightAxis(canvas);
		drawCHAxis(canvas);
		drawCVAxis(canvas);
		
		drawCTHAxis(canvas);
		drawCLVAxis(canvas);
		drawCBHAxis(canvas);
		drawCRVAxis(canvas);
		if(grid!=null){
			if(grid.getMaxValue()!=null){
				drawText(canvas, startX+4f, 24+4f, grid.getMaxValue(), Color.RED);
			}
			if(grid.getCenterValue()!=null){
				drawText(canvas, startX+4f, super.getHeight()/2, grid.getCenterValue(), Color.BLACK);
			}
			if(grid.getMingValue()!=null){
				drawText(canvas, startX+4f, height-14f, grid.getMingValue(), Color.GREEN);	
			}			
			if(grid.getRizeUp()!=null){
				drawText(canvas, width-90f, 24+4f, grid.getRizeUp(), Color.RED);
			}
			if(grid.getRizeUp()!=null){
				drawText(canvas, width-95f, height-14f, grid.getRizeDown(), Color.GREEN);
			}
			if(datas.size()>0){
				drawChart(canvas, datas);
			}
			if(scrollX!=0){
				if(scrollX<=chartWidth){
					drawCross(canvas, scrollX);
				}else{
					drawCross(canvas, chartWidth);
				}				
			}
		}		
	}
	
	/**
	 * 绘制可移动的竖线
	 * @param canvas
	 */
	protected void drawCross(Canvas canvas, float postX) {
		float length = super.getHeight();
		Paint mPaint = new Paint();
		mPaint.setStrokeWidth(3.5f);
		mPaint.setColor(Util.getResourcesColor(R.color.black));
		canvas.drawLine(postX, 0f, postX, length, mPaint);
		invalidate();
		vctView.setScrollX(postX);
	}
	
	private void drawChart(Canvas canvas, List<YBEntity> entitys){
		int size = entitys.size();
		for(int i=0; i<size-1; i++){
			drawLine(getPointX(i), getPointY(entitys.get(i)), getPointX(i+1), getPointY(entitys.get(i+1)), canvas);
		}
	}
	/**
	 * 获取X坐标
	 */
	private float getPointX(int size){	
		chartWidth = (size/720f)*width+startX;
		return chartWidth;
	}
	
	/**
	 * 获取Y坐标
	 */
	private float getPointY(YBEntity entity){
		float cur = Float.parseFloat(entity.getCur_prc());
		float max = Float.parseFloat(grid.getMaxValue());
		float min = Float.parseFloat(grid.getMingValue());
		
		return height-((cur-min)/(max-min)*height);
	}
	/**
	 * 画线
	 * @param pX 起点X坐标
	 * @param pY 起点Y坐标
	 * @param eX 终点X坐标
	 * @param eY 终点Y坐标
	 */	
	private void drawLine(float pX, float pY, float eX, float eY, Canvas canvas){
		Paint paint = new Paint();
		paint.setAntiAlias(true);//设置抗锯齿
		paint.setStrokeWidth(3.5f);
		paint.setColor(Util.getResourcesColor(R.color.black));
		canvas.drawLine(pX, pY, eX, eY, paint);
	}
	/**
	 * 绘制X轴
	 * 
	 * @param canvas
	 */
	protected void drawXAxis(Canvas canvas) {
		float postY = height - 1;
		Paint mPaint = new Paint();
		mPaint.setStrokeWidth(3.5f);
		mPaint.setColor(Util.getResourcesColor(R.color.lightgray));
		canvas.drawLine(startX, postY, width, postY, mPaint);
	}

	/**
	 * 绘制Y轴
	 * @param canvas
	 */
	protected void drawYAxis(Canvas canvas) {
		Paint mPaint = new Paint();
		mPaint.setStrokeWidth(3.5f);
		mPaint.setColor(Util.getResourcesColor(R.color.lightgray));
		canvas.drawLine(startX, 0f, startX, height, mPaint);
	}
	
	/**
	 * 绘制顶轴
	 * @param canvas
	 */
	protected void drawTopAxis(Canvas canvas) {
		Paint mPaint = new Paint();
		mPaint.setStrokeWidth(3.5f);
		mPaint.setColor(Util.getResourcesColor(R.color.lightgray));
		canvas.drawLine(startX, startY, width, startY, mPaint);
	}

	/**
	 * 绘制右边轴
	 * @param canvas
	 */
	protected void drawRightAxis(Canvas canvas) {
		float postX = super.getWidth()-startX;
		Paint mPaint = new Paint();
		mPaint.setStrokeWidth(3.5f);
		mPaint.setColor(Util.getResourcesColor(R.color.lightgray));
		canvas.drawLine(postX, 0f, postX, height, mPaint);		
	}
	
	/**
	 * 绘制中间横轴
	 * @param canvas
	 */
	protected void drawCHAxis(Canvas canvas) {

		float length = super.getWidth()-startX;
		float postY = super.getHeight()/2;
		Paint mPaint = new Paint();
		mPaint.setStrokeWidth(3.5f);
		mPaint.setColor(Util.getResourcesColor(R.color.lightgray));
		canvas.drawLine(startX, postY, length, postY, mPaint);
	}

	/**
	 * 绘制中间竖轴
	 * @param canvas
	 */
	protected void drawCVAxis(Canvas canvas) {

		float length = super.getHeight();
		float postX = super.getWidth()/2;
		Paint mPaint = new Paint();
		mPaint.setStrokeWidth(3.5f);
		mPaint.setColor(Util.getResourcesColor(R.color.lightgray));
		canvas.drawLine(postX, 0f, postX, length, mPaint);
	}
	
	/**
	 * 绘制中上横轴（虚线）
	 * @param canvas
	 */
	protected void drawCTHAxis(Canvas canvas) {

		float length = super.getWidth()-startX;
		float postY = super.getHeight()/4;
		Path mPath = new Path();
	    mPath.moveTo(startX, postY);
	    mPath.quadTo(startX, postY, length, postY);	    
		Paint mPaint = new Paint();
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(2.0f);
		mPaint.setColor(axisColor);
		PathEffect effects = new DashPathEffect(new float[] { 1, 2, 4, 8}, 1);
		mPaint.setPathEffect(effects);
		canvas.drawPath(mPath, mPaint);
	}

	/**
	 * 绘制中左间竖轴（虚线）
	 * @param canvas
	 */
	protected void drawCLVAxis(Canvas canvas) {
		float length = super.getHeight();
		float postX = super.getWidth()/4;
		Path mPath = new Path();
	    mPath.moveTo(postX, 0f);
	    mPath.quadTo(postX, 0f, postX, length);
		Paint mPaint = new Paint();
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(2.0f);
		mPaint.setColor(axisColor);
		PathEffect effects = new DashPathEffect(new float[] { 1, 2, 4, 8}, 1);  
		mPaint.setPathEffect(effects);
		canvas.drawPath(mPath, mPaint);
	}
	
	/**
	 * 绘制中上横轴（虚线）
	 * @param canvas
	 */
	protected void drawCBHAxis(Canvas canvas) {

		float length = super.getWidth()-startX;
		float postY = (super.getHeight()/4)*3;
		Path mPath = new Path();
	    mPath.moveTo(startX, postY);
	    mPath.quadTo(startX, postY, length, postY);
		Paint mPaint = new Paint();
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(2.0f);
		mPaint.setColor(axisColor);
		PathEffect effects = new DashPathEffect(new float[] { 1, 2, 4, 8}, 1);
		mPaint.setPathEffect(effects);
		canvas.drawPath(mPath, mPaint);
	}

	/**
	 * 绘制中左间竖轴（虚线）
	 * @param canvas
	 */
	protected void drawCRVAxis(Canvas canvas) {

		float length = super.getHeight();
		float postX = (super.getWidth()/4)*3;
		Path mPath = new Path();
	    mPath.moveTo(postX, 0f);
	    mPath.quadTo(postX, 0f, postX, length);
		Paint mPaint = new Paint();
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(2.0f);
		mPaint.setColor(axisColor);
		PathEffect effects = new DashPathEffect(new float[] { 1, 2, 4, 8}, 1);  
		mPaint.setPathEffect(effects);
		canvas.drawPath(mPath, mPaint);
	}
	
	public void drawText(Canvas canvas, float x, float y, String str, int color){
		Paint p = new Paint();   
	    p.setColor(color);   
	    p.setAntiAlias(true);//去除锯齿  
	    p.setFilterBitmap(true);//对位图进行滤波处理  
	    p.setTextSize(32);  
	    canvas.drawText(str,x,y,p);
	}
	
	private int getScrollIndex(){
		int index = (int) ((scrollX/width)*720f);
		return index;
	}
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		getParent().requestDisallowInterceptTouchEvent(true); 
		switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				scrollX = event.getX();
				invalidate();
				break;
	
			case MotionEvent.ACTION_MOVE:
				scrollX = event.getX();
				//重绘
				invalidate();	
				//发送广播通知界面更改文本值
				Intent intent = new Intent("COM.ZLWY.CHARTMOVE");
				intent.putExtra("INDEX", getScrollIndex());
				if(context!=null){
					context.sendBroadcast(intent);
				}	
				break;
				
			case MotionEvent.ACTION_UP:
				scrollX = 0;
				vctView.setScrollX(0f);
				break;	
		}
		return true;
	}
}
